package com.ruoyi.fitsculpt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fitsculpt.domain.EasyfitDietPlans;
import com.ruoyi.fitsculpt.service.IEasyfitDietPlansService;
import com.ruoyi.fitsculpt.domain.EasyfitUsers;
import com.ruoyi.fitsculpt.service.IEasyfitUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 饮食计划Controller
 *
 * @author metaee
 * @date 2025-02-27
 */
@Api(tags = "饮食计划管理")
@RestController
@RequestMapping("/fitsculpt/diet/plans")
public class EasyfitDietPlansController extends BaseController {
    @Autowired
    private IEasyfitDietPlansService easyfitDietPlansService;

    @Autowired
    private IEasyfitUsersService easyfitUsersService;

    /**
     * 查询饮食计划列表
     */
    @ApiOperation("查询饮食计划列表")
    @GetMapping("/list")
    public TableDataInfo list(EasyfitDietPlans easyfitDietPlans) {
        startPage();
        List<EasyfitDietPlans> list = easyfitDietPlansService.selectEasyfitDietPlansList(easyfitDietPlans);
        for (EasyfitDietPlans plan : list) {
            if (plan.getUserId() != null) {
                EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(plan.getUserId());
                plan.setUser(user);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出饮食计划列表
     */
    @ApiOperation("导出饮食计划列表")
    @Log(title = "饮食计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasyfitDietPlans easyfitDietPlans) {
        List<EasyfitDietPlans> list = easyfitDietPlansService.selectEasyfitDietPlansList(easyfitDietPlans);
        ExcelUtil<EasyfitDietPlans> util = new ExcelUtil<EasyfitDietPlans>(EasyfitDietPlans.class);
        util.exportExcel(response, list, "饮食计划数据");
    }

    /**
     * 获取饮食计划详细信息
     */
    @ApiOperation("获取饮食计划详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        EasyfitDietPlans plan = easyfitDietPlansService.selectEasyfitDietPlansById(id);
        if (plan != null && plan.getUserId() != null) {
            EasyfitUsers user = easyfitUsersService.selectEasyfitUsersById(plan.getUserId());
            plan.setUser(user);
        }
        return success(plan);
    }

    /**
     * 新增饮食计划
     */
    @ApiOperation("新增饮食计划")
    @Log(title = "饮食计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EasyfitDietPlans easyfitDietPlans) {
        return toAjax(easyfitDietPlansService.insertEasyfitDietPlans(easyfitDietPlans));
    }

    /**
     * 修改饮食计划
     */
    @ApiOperation("修改饮食计划")
    @Log(title = "饮食计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EasyfitDietPlans easyfitDietPlans) {
        return toAjax(easyfitDietPlansService.updateEasyfitDietPlans(easyfitDietPlans));
    }

    /**
     * 删除饮食计划
     */
    @ApiOperation("删除饮食计划")
    @Log(title = "饮食计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public AjaxResult remove(@ApiParam(value = "饮食计划ID数组", required = true) @PathVariable Long[] planIds) {
        return toAjax(easyfitDietPlansService.deleteEasyfitDietPlansByPlanIds(planIds));
    }
}
